﻿// ********************************************************************
//  文件名: MyUdpClient.cs
//  描述:
//  作者: 胡宇辰
//  创建时间: 2024-3-19
// 
//  修改历史:
// ********************************************************************

using System.Net;
using System.Net.Sockets;
using System.Text;

namespace MyApp;

public class MyUdpServer
{
    public static void Start()
    {
        UdpClient server = new UdpClient(6324);
        Console.WriteLine("Server started. Waiting for messages...");

        while (true)
        {
            IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
            byte[] receivedData = server.Receive(ref clientEndPoint);
            string receivedMessage = Encoding.UTF8.GetString(receivedData);

            Console.WriteLine($"Message received from {clientEndPoint}: {receivedMessage}");

            // Echo back to client
            server.Send(receivedData, receivedData.Length, clientEndPoint);
            Console.WriteLine("Echo sent to client.");
        }
    }
}